package com.songtech.uitest.components;

import com.songtech.uitest.resources.SystemResource;
import com.songtech.uitest.util.JarPathHelper;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.ie.InternetExplorerOptions;
import org.openqa.selenium.opera.OperaDriver;
import org.openqa.selenium.opera.OperaOptions;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.UnsupportedEncodingException;

@EnableConfigurationProperties(SystemResource.class)
@Component
public class BrowserDriver implements IBrowserDriverFactory {

    private RemoteWebDriver mainDriver = null;
    private RemoteWebDriver viceDriver = null;
    private RemoteWebDriver thirdDriver = null;

    @Autowired
    @Qualifier("systemResource")
    private SystemResource systemResource;

    @Autowired
    private JarPathHelper jarPathHelper;




    private RemoteWebDriver getDriver(String driverName) {
        RemoteWebDriver driver = null;
        if(driverName.equals("firefox")){
//            https://blog.csdn.net/dunceiam/article/details/42740537
//            String driverPath = BrowserDriver.class.getResource("/").getFile() + systemResource.getFirefoxDriverFile();
            //为了使工程打包成jar包也可以跑，改成了如下：
            String driverPath = jarPathHelper.getWebDriverFilePath(systemResource.getFirefoxDriverFile());
            String fireFoxPath = systemResource.getFirefoxPath();
            System.setProperty("webdriver.gecko.driver", driverPath);
            System.setProperty("webdriver.firefox.bin", fireFoxPath);

            Proxy pxy = new Proxy();
            pxy.setProxyType(Proxy.ProxyType.AUTODETECT);
            FirefoxOptions options = new FirefoxOptions().setProxy(pxy);
            driver = new FirefoxDriver(options);
        }
        if(driverName.equals("chrome")){
//            String driverPath = BrowserDriver.class.getResource("/").getFile() + systemResource.getChromeDriverFile();
            String driverPath = jarPathHelper.getWebDriverFilePath(systemResource.getChromeDriverFile());
            String chromePath = systemResource.getChromePath();
            System.setProperty("webdriver.chrome.driver", driverPath);
            Proxy pxy = new Proxy();
            pxy.setProxyType(Proxy.ProxyType.AUTODETECT);
            ChromeOptions options = new ChromeOptions().setProxy(pxy);
            options.addArguments("start-maximized");
            options.setBinary(chromePath);
            driver = new ChromeDriver(options);
        }
        if(driverName.equals("ie")){
//            String driverPath = BrowserDriver.class.getResource("/").getFile() + systemResource.getIeDriverFile();
            String driverPath = jarPathHelper.getWebDriverFilePath(systemResource.getIeDriverFile());
            String iePath = systemResource.getIePath();
            System.setProperty("webdriver.ie.driver", driverPath);
            System.setProperty("webdriver.ie.bin", iePath);
            Proxy pxy = new Proxy();
            pxy.setProxyType(Proxy.ProxyType.AUTODETECT);
            InternetExplorerOptions options = new InternetExplorerOptions().setProxy(pxy);
            driver = new InternetExplorerDriver(options);
        }
        if(driverName.equals("opera")){
//            String driverPath = BrowserDriver.class.getResource("/").getFile() + systemResource.getOperaDriverFile();
            String driverPath = jarPathHelper.getWebDriverFilePath(systemResource.getOperaDriverFile());
            String operaPath = systemResource.getOperaPath();
            System.setProperty("webdriver.opera.driver", driverPath);
            Proxy pxy = new Proxy();
            pxy.setProxyType(Proxy.ProxyType.AUTODETECT);
            OperaOptions options = new OperaOptions().setProxy(pxy);
            options.setBinary(operaPath);
            options.addArguments("start-maximized");
            driver = new OperaDriver(options);

        }

        return driver;
    }


    public RemoteWebDriver getMainDriver(){
        String driverName = systemResource.getMainBrowser();
        if(mainDriver == null) {
            mainDriver = getDriver(driverName);
        }
        return mainDriver;
    }

    public RemoteWebDriver getViceDriver(){
        String driverName = systemResource.getViceBrowser();
        if(viceDriver == null) {
            viceDriver = getDriver(driverName);
        }
        return viceDriver;
    }

    public RemoteWebDriver getThirdDriver(){
        String driverName = systemResource.getThirdBrowser();
        if(thirdDriver == null) {
            thirdDriver = getDriver(driverName);
        }
        return thirdDriver;
    }


    @Override
    public void closeDriver(RemoteWebDriver driver) throws Exception {
        Thread.sleep(3000);
        driver.quit();
    }

    @Override
    public void closeAllDriver() throws Exception {
        Thread.sleep(3000);
        if(mainDriver != null){
            mainDriver.quit();
        }
        if(viceDriver != null){
            viceDriver.quit();
        }
        if(thirdDriver != null){
            thirdDriver.quit();
        }

    }




}
